var Slider={
	init: function(config){
			this.container = config.container;
			this.divs = this.container.find('div.slider_image');
			this.divsWidth = config.divWidth; 
			this.divsLen = this.divs.length;
			this.nav = config.nav;
			this.speed = config.speed;
			this.animation = config.animation;
			this.refreshRate = config.refreshRate;
			this.current = 0;
			
			console.log(this.container);
			console.log(this.nav);
			this.thumbHighlight();
			if(config.refresAvaiable === true){
				this.refresh();	
			}
			this.bindEvents();
	},

	bindEvents: function(){
			var self = this;
			console.log(self.nav.children('button'));
			self.nav.children('button').on('click',self.execute);
	},

	execute: function(refreshing){
			var self = Slider;
			console.log((refreshing) ? 'next' :$(this).data('dir'));
			self.setCurrent((refreshing ===1) ? 'next' :$(this).data('dir'));
			self.transition();
			
	},

	transition: function( coords ){
			var self = Slider;
			if(self.animation === 'slide'){
				self.thumbHighlight();
				self.container.animate({
					'margin-left': coords || -( self.current * self.divsWidth )
				},{duration : self.speed,
				   
				  });
			}
			if(self.animation === 'fade'){
				self.container.animate({
									'opacity': 'hide'
								},{
									duration : self.speed,
								    complete : function(){
								   		 self.container.css('margin-left',coords || -( self.current * self.divsWidth ));
								   		 self.thumbHighlight();
								    }
								  }
								).animate({
									'opacity': 'show'
								},{
									duration : self.speed,
								     
								  }
								)	
			}
			
		
	},

	setCurrent: function( dir ){
				var self = Slider;
				var pos = self.current;
				(dir === 'next') ? pos++ :
				(dir === 'prev') ? pos-- : pos = dir;
				
				self.current = ( pos < 0 ) ? self.divsLen - 1 : pos % self.divsLen;
			
	},

	thumbHighlight: function(){
		var self = Slider;
		var buttons = self.nav.find('span.badge');


	
		buttons.each(function(){

			if($(this).removeClass('badge-info').addClass('badge-inverse').data('dir') === self.current){
				$(this).removeClass('badge-inverse').addClass('badge-info');
			}

		
		});
	},

	refresh: function(){
		var self = Slider;
		setTimeout(function() {
				self.execute(1);
				
				self.refresh();
			},  self.refreshRate);
	}

};

Slider.init({

	container : $('.slider ul'), 		// container of divs
	nav : $('div.slider_nav'), 			// container with nav buttons
	divWidth : 820, 					//width of div
	speed : 500,
	animation : 'fade', 				// 'slide' or 'fade'
	refresAvaiable : true,
	refreshRate : 5000
	
});








